package demisemapping;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class GameLocator implements ImageNode {

    final int NUMBER_OF_CAPTURES = 2;
    final int[] CAPTURE_W = {90, 10}, CAPTURE_H = {120, 10};
    final int[] CAPTURE_X = {424, 506}, CAPTURE_Y = {414, 33};
    final int[] CAPTURE_X_WIDE = {424, 393}, CAPTURE_Y_WIDE = {414, 34};
    final int[] CAPTURE_X_FULL = {40, 393}, CAPTURE_Y_FULL = {26, 51};
    final int[][] CAPTURE_X_ARRAYS = {CAPTURE_X, CAPTURE_X_WIDE, CAPTURE_X_FULL, CAPTURE_X, CAPTURE_X_WIDE, CAPTURE_X_FULL};
    final int[][] CAPTURE_Y_ARRAYS = {CAPTURE_Y, CAPTURE_Y_WIDE, CAPTURE_Y_FULL, CAPTURE_Y, CAPTURE_Y_WIDE, CAPTURE_Y_FULL};
    
    final int w = Main.SCREEN_WIDTH, h = Main.SCREEN_HEIGHT;
    
    public static final int MODE_WINDOWED = 0;
    public static final int MODE_WINDOWED_WIDE = 1;
    public static final int MODE_WINDOWED_FULL = 2;
    public static final int MODE_FULLSCREEN = 3;
    public static final int MODE_FULLSCREEN_WIDE = 4;
    public static final int MODE_FULLSCREEN_FULL = 5;
    
    ArrayList<GameNode> nodes = new ArrayList<GameNode>();
    
    public void notifyNodes(int mode, ArrayList<BufferedImage> images, ArrayList<int[]> shots, int[] CAPTURE_W, int[] CAPTURE_H) {
        for (GameNode node : nodes) {
            node.notifyOfGame(mode, images, shots, CAPTURE_W, CAPTURE_H);
        }
    }
    
    public void notifyReady() {
        
        // find top left of game window
        // [defined by a sequence of pixels]
        
        int[] bytes = {74,59,39,87,71,46,78,64,43};
        int[] widebytes = {57,42,24,70,48,28,74,51,29};
        int[] fullbytes = {75,61,32,74,58,33,67,51,33};
        int[] fsbytes = {74,56,33,82,69,41,74,65,41};
        int[] fswidebytes = {57,40,24,74,48,24,74,52,33};
        int[] fsfullbytes = {74,60,33,74,56,33,66,52,33};
        int[][] arrays = {bytes, widebytes, fullbytes, fsbytes, fswidebytes, fsfullbytes};
        
        final int SCREENSHOT_W = 300, SCREENSHOT_H = 100;
        int px[] = new int[3*SCREENSHOT_W*SCREENSHOT_H];
        Main.bi = Main.r.createScreenCapture(new Rectangle(0, 0, SCREENSHOT_W, SCREENSHOT_H));
        assert(Main.bi != null);
        Main.bi.getData().getPixels(0, 0, SCREENSHOT_W, SCREENSHOT_H, px);
        
        for (int i = 0; i < SCREENSHOT_H; i++) {
            for (int j = 0; j < SCREENSHOT_W - bytes.length + 1; j++) {
                
                boolean good = true;
                
                int arrIndex = 0;
                for (; arrIndex < arrays.length; ++arrIndex) {
                    
                    // check for a match with arrays[arrIndex]
                    
                    good = true;
                    for (int k = 0; good && k < bytes.length; k++) {
                        if (px[3*(SCREENSHOT_W*i+j)+k] != arrays[arrIndex][k]) {
                            good = false;
                            break;
                        }
                    }
                    
                    if (good) break;
                }
                
//                // check for windowed mode
//                for (int k = 0; good && k < bytes.length; k++) {
//                    if (px[3*(SCREENSHOT_W*i+j)+k] != bytes[k]) good = false;
//                }
//                
//                // check for fullscreen mode
//                if (!good) {
//                    good = true;
//                    for (int k = 0; good && k < fsbytes.length; k++) {
//                        if (px[3*(SCREENSHOT_W*i+j)+k] != fsbytes[k]) good = false;
//                    }
//                }
//                
//                for (int k=0;k<9;k++) System.out.print(px[3*(SCREENSHOT_W*0+0)+k] + (k < 8 ? "," : "\n"));
                
                if (good) {
//                    System.out.println("Game window found at ("+j+","+i+") in mode " + arrIndex);
                    //for (int k=0;k<9;k++) System.out.print(px[3*(SCREENSHOT_W*i+j)+k] + (k < 8 ? "," : "\n"));
                    int x = j, y = i;
                    ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
                    ArrayList<int[]> shots = new ArrayList<int[]>();
                    for (int ii=0;ii<NUMBER_OF_CAPTURES;ii++) {
                        Main.bi = Main.r.createScreenCapture(new Rectangle(
                                x+CAPTURE_X_ARRAYS[arrIndex][ii], y+CAPTURE_Y_ARRAYS[arrIndex][ii],
                                CAPTURE_W[ii], CAPTURE_H[ii]));
                        int[] _px = new int[CAPTURE_W[ii]*CAPTURE_H[ii]*3];
                        Main.bi.getData().getPixels(
                                0, 0, CAPTURE_W[ii], CAPTURE_H[ii], _px);
                        images.add(Main.bi);
                        shots.add(_px);
                    }
                    notifyNodes(arrIndex, images, shots, CAPTURE_W, CAPTURE_H);
                    return;
                }
            }
        }
    }
    
}
